export default class EleUtils {
    /**
     * 滚动到指定元素
     * @param vm 组件实例
     * @param vmId 在组件里面的id
     * @param time
     * @param offset 偏移量 距离顶部相加
     */
    static showEle(vm, vmId, time = 300, offset = 0) {
        EleUtils.eleIsNotInView(vm, vmId, {
            offsetTop: offset
        }).then((ele) => {
            // 如果是支付宝
            //#ifdef MP-ALIPAY
            console.log('不支持')
            //#endif
            // 不是支付宝
            //#ifndef MP-ALIPAY
            const setTimer = setTimeout(() => {
                //3-2、开始滚动
                uni.pageScrollTo({
                    scrollTop: ele.top + offset,
                    duration: time,
                });
                clearTimeout(setTimer)
            })
            //#endif
        })
    }

    /**
     *
     * @param vm
     * @param vmId
     * @param offset {EleUtilsDefaultConfif_eleIsNotInView_offset}
     * @return {Promise<never>|Promise<unknown>}
     */
    static eleIsNotInView(vm, vmId,
                          offset = EleUtilsDefaultConfif_eleIsNotInView_offset) {
        const ele = EleUtils.getEleInfo(vm, vmId)
        if (ele === null) {
            console.error('获取元素失败')
            return Promise.reject(null)
        }
        offset = Object.assign(new EleUtilsDefaultConfif_eleIsNotInView_offset(), offset)
        return new Promise((resolve, reject) => {
            const setTimer = setTimeout(() => {
                uni.getSystemInfo({
                    success: ({windowHeight, windowWidth}) => {
                        if (
                            ele.top >= offset.offsetTop &&
                            ele.left >= offset.offsetLeft &&
                            ele.bottom <=
                            windowHeight - offset.offsetTottom &&
                            ele.right <=
                            windowWidth - offset.offsetRight
                        ) {
                            reject(null)
                        } else {
                            resolve(ele)
                        }
                    },
                    fail: () => {
                        reject(null)
                    }
                })
                clearTimeout(setTimer)
            })
        })
    }

    static getEleInfo(vm, vmId) {
        const specialSymbols = ['[', ']', '.']
        const find = specialSymbols.find(i => vmId.indexOf(i) !== -1);
        if (find) {
            console.error('存在特殊符号: ' + find)
            return null
        }
        let result = {
            bottom: 0,
            dataset: {},
            height: 0,
            id: "",
            left: 0,
            right: 0,
            top: 0,
            width: 0,
        }
        const query = uni.createSelectorQuery().in(vm);
        query.select('#' + vmId).boundingClientRect(data => {
            result = data
        }).exec();
        return result
    }
}

export class EleUtilsDefaultConfif_eleIsNotInView_offset {
    offsetTop = 0
    offsetTottom = 0
    offsetLeft = 0
    offsetRight = 0
}